#include "../mqserver/mq_consume.hpp"
#include <gtest/gtest.h>

void cb(const std::string& ctag,const mq::BasicProperties* bp,const std::string& qname)
{
    std::cout << "从" << qname << "中取出" << ctag << std::endl;
}

mq::ConsumerManager::Ptr cmp;
class My_Consumer_Test : public testing::Environment 
{
public:
    void SetUp() override
    {
        cmp = std::make_shared<mq::ConsumerManager>();
        cmp->initQueueConsumer("queue1");
        cmp->initQueueConsumer("queue2");
        cmp->initQueueConsumer("queue3");

        cmp->create("consumer1","queue1",false,cb);
        cmp->create("consumer2","queue1",false,cb);
        cmp->create("consumer3","queue1",false,cb);

        cmp->create("consumer1","queue2",false,cb);
        cmp->create("consumer2","queue2",false,cb);
        cmp->create("consumer3","queue2",false,cb);

        cmp->create("consumer1","queue3",false,cb);
        cmp->create("consumer2","queue3",false,cb);
        cmp->create("consumer3","queue3",false,cb);
    }
    void TearDown() override
    {
        cmp->clear();
    }
private:
    ;
};

TEST(My_CONSUMER_Test,insert_test)
{
    ASSERT_EQ(cmp->exists("consumer1","queue1"),true);
    ASSERT_EQ(cmp->exists("consumer2","queue1"),true);
    ASSERT_EQ(cmp->exists("consumer3","queue1"),true);

    ASSERT_EQ(cmp->exists("consumer1","queue2"),true);
    ASSERT_EQ(cmp->exists("consumer2","queue2"),true);
    ASSERT_EQ(cmp->exists("consumer3","queue2"),true);

    ASSERT_EQ(cmp->exists("consumer1","queue3"),true);
    ASSERT_EQ(cmp->exists("consumer2","queue3"),true);
    ASSERT_EQ(cmp->exists("consumer3","queue3"),true);
}

// TEST(My_CONSUMER_Test,choose_test)
// {
//     mq::Consumer::Ptr cp = cmp->choose("queue1");
//     ASSERT_NE(cp.get(),nullptr);
//     ASSERT_EQ(cp->tag,std::string("consumer2"));

//     cp = cmp->choose("queue1");
//     ASSERT_NE(cp.get(),nullptr);
//     ASSERT_EQ(cp->tag,std::string("consumer3"));

//     cp = cmp->choose("queue1");
//     ASSERT_NE(cp.get(),nullptr);
//     ASSERT_EQ(cp->tag,std::string("consumer1"));
// }

TEST(My_CONSUMER_Test,remove_test)
{
    cmp->remove("consumer1","queue1");
    cmp->remove("consumer2","queue1");
    cmp->remove("consumer3","queue1");

    mq::Consumer::Ptr cp = cmp->choose("queue1");
    ASSERT_EQ(cp.get(),nullptr);
    ASSERT_EQ(cmp->empty("queue1"),true);

    cmp->remove("consumer1","queue2");
    cmp->remove("consumer2","queue2");
    cmp->remove("consumer3","queue2");

    cp = cmp->choose("queue2");
    ASSERT_EQ(cp.get(),nullptr);
    ASSERT_EQ(cmp->empty("queue2"),true);

    cmp->remove("consumer1","queue3");
    cmp->remove("consumer2","queue3");
    cmp->remove("consumer3","queue3");

    cp = cmp->choose("queue3");
    ASSERT_EQ(cp.get(),nullptr);
    ASSERT_EQ(cmp->empty("queue3"),true);
}

TEST(My_CONSUMER_Test,destroy_test)
{
    ASSERT_EQ(cmp->size(),3);
    cmp->destoryQueueConsumer("queue1");
    ASSERT_EQ(cmp->size(),2);
    cmp->destoryQueueConsumer("queue2");
    ASSERT_EQ(cmp->size(),1);
    cmp->destoryQueueConsumer("queue3");
    ASSERT_EQ(cmp->size(),0);
}

int main(int argc,char* argv[])
{
    testing::InitGoogleTest(&argc,argv);
    testing::AddGlobalTestEnvironment(new My_Consumer_Test);
    RUN_ALL_TESTS();

    return 0;
}